package com.irate.fragment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.irate.activity.R;
import com.irate.adapter.FriendRequestAdapter;
import com.irate.cache.ImageCache.ImageCacheParams;
import com.irate.cache.ImageFetcher;
import com.irate.dao.FriendRequestDAO;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.model.FriendRequest;

public class FriendRequestFriend extends Fragment {

	protected ListView list;
	protected FriendRequestDAO frdao;
	protected List<FriendRequest> listFriendSendRequest = null;
	protected List<FriendRequest> listFriendIsRequest = null;

	protected ImageFetcher imageFetcher;
	protected static final String IMAGE_CACHE_DIR = "thumbs";
	protected int mImageThumbSize = 100;

	protected ProgressDialog dialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.friend_request_friend, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		init();
		execute();

	}

	@Override
	public void onResume() {
		super.onResume();
		imageFetcher.setExitTasksEarly(false);
	}

	@Override
	public void onPause() {
		super.onPause();
		imageFetcher.setPauseWork(false);
		imageFetcher.setExitTasksEarly(true);
		imageFetcher.flushCache();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		imageFetcher.closeCache();
	}

	private void init() {
		frdao = new FriendRequestDAO();
		list = (ListView) getActivity().findViewById(R.id.list_friend_request);

		ImageCacheParams cacheParams = new ImageCacheParams(getActivity(),
				IMAGE_CACHE_DIR);

		cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of
		imageFetcher = new ImageFetcher(getActivity(), mImageThumbSize);
		imageFetcher.setLoadingImage(R.drawable.user);
		imageFetcher.addImageCache(
				((FragmentActivity) getActivity()).getSupportFragmentManager(),
				cacheParams);
	}

	private void execute() {

		dialog = ProgressDialog.show(getActivity(),
				getResources().getString(R.string.friend_request),
				getResources().getString(R.string.loading), true);

		frdao.getListSendRequest(handlerListSendFriend, getActivity(), dialog);
		frdao.getListIsRequest(handlerListIsRequestFriend, getActivity(),
				dialog);

	}

	private void fillDataToListView() {
		if (listFriendIsRequest != null && listFriendSendRequest != null) {
			FriendRequest title_sendRequest = new FriendRequest();
			title_sendRequest.setUser_name("Requests ("
					+ listFriendIsRequest.size() + ")");

			title_sendRequest.setIsMock(true);

			List<FriendRequest> ldata = new ArrayList<FriendRequest>();
			ldata.add(title_sendRequest);

			ldata.addAll(listFriendIsRequest);

			FriendRequest title_isRequest = new FriendRequest();
			title_isRequest.setUser_name("Sent Requests ("
					+ listFriendSendRequest.size() + ")");
			title_isRequest.setIsMock(true);

			ldata.add(title_isRequest);

			ldata.addAll(listFriendSendRequest);

			FriendRequestAdapter fra = new FriendRequestAdapter(ldata,
					imageFetcher, getActivity());

			list.setAdapter(fra);

			list.setOnScrollListener(new OnScrollListener() {

				@Override
				public void onScrollStateChanged(AbsListView view,
						int scrollState) {
					if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
						imageFetcher.setPauseWork(true);
					} else {
						imageFetcher.setPauseWork(false);
					}
				}

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem,
						int visibleItemCount, int totalItemCount) {
					// TODO Auto-generated method stub

				}
			});

			if (dialog != null)
				dialog.dismiss();

		}
	}

	AsyncHttpResponseHandler handlerListSendFriend = new AsyncHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] response) {
			System.out.println("Success register");

			listFriendSendRequest = frdao.parseData(new String(response), "to");

		}

		@Override
		public void onStart() {
			System.out.println("Start");
		}

		@Override
		public void onFinish() {
			fillDataToListView();
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] errorResponse, Throwable e) {
			System.out.println("Fail");
			listFriendSendRequest = new ArrayList<FriendRequest>();

		}
	};

	AsyncHttpResponseHandler handlerListIsRequestFriend = new AsyncHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] response) {
			System.out.println("Success register");

			listFriendIsRequest = frdao.parseData(new String(response), "from");
		}

		@Override
		public void onStart() {
			System.out.println("Start");
		}

		@Override
		public void onFinish() {
			fillDataToListView();
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] errorResponse, Throwable e) {
			System.out.println("Fail");
			listFriendIsRequest = new ArrayList<FriendRequest>();

		}
	};

}